package com.billManage.web.controller.system;

import java.util.Date;
import java.util.List;

import com.billManage.system.domain.SysBill;
import com.billManage.system.service.ISysBillService;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.billManage.common.annotation.Log;
import com.billManage.common.enums.BusinessType;
import com.billManage.system.domain.SysBillApply;
import com.billManage.system.service.ISysBillApplyService;
import com.billManage.common.core.controller.BaseController;
import com.billManage.common.core.domain.AjaxResult;
import com.billManage.common.utils.poi.ExcelUtil;
import com.billManage.common.core.page.TableDataInfo;

/**
 * 票据报销申请Controller
 * 
 * @author ruoyi
 * @date 2024-05-20
 */
@Controller
@RequestMapping("/system/apply")
public class SysBillApplyController extends BaseController
{
    private String prefix = "system/apply";


    @Autowired
    private ISysBillApplyService sysBillApplyService;


    @Autowired
    private ISysBillService sysBillService;

    @RequiresPermissions("system:apply:view")
    @GetMapping()
    public String apply()
    {
        return prefix + "/apply";
    }

    @RequiresPermissions("system:apply:viewCw")
    @GetMapping("/Cw")
    public String applyCw()
    {
        return prefix + "/applyCw";
    }



    /**
     * 查询票据报销申请列表
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SysBillApply sysBillApply)
    {
        startPage();
        sysBillApply.setCreateBy(this.getLoginName());
        List<SysBillApply> list = sysBillApplyService.selectSysBillApplyList(sysBillApply);
        return getDataTable(list);
    }

    /**
     * 查询票据报销申请列表
     */
    @PostMapping("/listCw")
    @ResponseBody
    public TableDataInfo listCw(SysBillApply sysBillApply)
    {
        startPage();
        List<SysBillApply> list = sysBillApplyService.selectSysBillApplyList(sysBillApply);
        return getDataTable(list);
    }

    /**
     * 导出票据报销申请列表
     */
    @Log(title = "票据报销申请", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SysBillApply sysBillApply)
    {
        List<SysBillApply> list = sysBillApplyService.selectSysBillApplyList(sysBillApply);
        ExcelUtil<SysBillApply> util = new ExcelUtil<SysBillApply>(SysBillApply.class);
        return util.exportExcel(list, "票据报销申请数据");
    }

    /**
     * 新增票据报销申请
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        SysBill sysBill = new SysBill();
        sysBill.setCreateBy(this.getLoginName());
        List<SysBill> sysBills =  this.sysBillService.selectSysBillList(sysBill);
        mmap.put("sysBills",sysBills);
        return prefix + "/add";
    }

    /**
     * 新增保存票据报销申请
     */
    @Log(title = "票据报销申请", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SysBillApply sysBillApply)
    {
        sysBillApply.setCreateBy(this.getLoginName());
        return toAjax(sysBillApplyService.insertSysBillApply(sysBillApply));
    }

    /**
     * 修改票据报销申请
     */
    @GetMapping("/edit/{billRecordId}")
    public String edit(@PathVariable("billRecordId") Long billRecordId, ModelMap mmap)
    {
        SysBillApply sysBillApply = sysBillApplyService.selectSysBillApplyByBillRecordId(billRecordId);
        mmap.put("sysBillApply", sysBillApply);
        SysBill sysBill = new SysBill();
        sysBill.setCreateBy(this.getLoginName());
        List<SysBill> sysBills =  this.sysBillService.selectSysBillList(sysBill);
        mmap.put("sysBills", sysBills);
        return prefix + "/edit";
    }

    /**
     * 财务报销审核
     */
    @GetMapping("/editCw/{billApplyId}")
    public String editCw(@PathVariable("billApplyId") Long billApplyId, ModelMap mmap)
    {
        SysBillApply sysBillApply = sysBillApplyService.selectSysBillApplyByBillRecordId(billApplyId);
        mmap.put("sysBillApply", sysBillApply);
        SysBill sysBill = sysBillService.selectSysBillByBillId(sysBillApply.getBillId());
        mmap.put("sysBill", sysBill);
        return prefix + "/editCw";
    }

    /**
     * 修改保存票据报销申请
     */
    @Log(title = "票据报销申请", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SysBillApply sysBillApply)
    {
        sysBillApply.setUpdateBy(this.getLoginName());
        return toAjax(sysBillApplyService.updateSysBillApply(sysBillApply));
    }

    /**
     * 修改保存票据报销申请
     */
    @Log(title = "票据报销申请", businessType = BusinessType.UPDATE)
    @PostMapping("/editCw")
    @ResponseBody
    public AjaxResult editCwSave(SysBillApply sysBillApply)
    {
        if(sysBillApply.getStatus().equals("2") && StringUtils.isEmpty(sysBillApply.getReason())){
            return   AjaxResult.error("请填写审核未通过原因！");
        }
        sysBillApply.setReviewBy(this.getLoginName());
        sysBillApply.setReviewTime(new Date());
        return toAjax(sysBillApplyService.updateSysBillApplyCw(sysBillApply));
    }



    /**
     * 删除票据报销申请
     */
    @Log(title = "票据报销申请", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(Long ids)
    {
        return toAjax(sysBillApplyService.deleteSysBillApplyByBillRecordId(ids));
    }
}
